package pku;

import java.util.Map;

public class Util
{
    public static final String DATA_PATH = "data/";

    public static String headerToString(KeyValue header, String seperator)
    {
        Map<String, Object> map = header.getMap();
        return String.valueOf(map.getOrDefault(MessageHeader.MESSAGE_ID, "0")) + seperator +
                map.getOrDefault(MessageHeader.TOPIC, "0") + seperator +
                map.getOrDefault(MessageHeader.BORN_TIMESTAMP, "0") + seperator +
                map.getOrDefault(MessageHeader.BORN_HOST, "0") + seperator +
                map.getOrDefault(MessageHeader.STORE_TIMESTAMP, "0") + seperator +
                map.getOrDefault(MessageHeader.STORE_HOST, "0") + seperator +
                map.getOrDefault(MessageHeader.START_TIME, "0") + seperator +
                map.getOrDefault(MessageHeader.STOP_TIME, "0") + seperator +
                map.getOrDefault(MessageHeader.TIMEOUT, "0") + seperator +
                map.getOrDefault(MessageHeader.PRIORITY, "-1") + seperator +
                map.getOrDefault(MessageHeader.RELIABILITY, "0") + seperator +
                map.getOrDefault(MessageHeader.SEARCH_KEY, "0") + seperator +
                map.getOrDefault(MessageHeader.SCHEDULE_EXPRESSION, "0") + seperator +
                map.getOrDefault(MessageHeader.SHARDING_KEY, "0") + seperator +
                map.getOrDefault(MessageHeader.SHARDING_PARTITION, "0") + seperator +
                map.getOrDefault(MessageHeader.TRACE_ID, "0");
    }

    public static KeyValue loadHeader(String[] elems)
    {
        if (elems.length != 16)
        {
            return null;
        }

        KeyValue kv = new DefaultKeyValue();

        if (!elems[0].equals("0"))
            kv.put(MessageHeader.MESSAGE_ID, elems[0]);

        if (!elems[1].equals("0"))
            kv.put(MessageHeader.TOPIC, elems[1]);

        if (!elems[2].equals("0"))
            kv.put(MessageHeader.BORN_TIMESTAMP, elems[2]);

        if (!elems[3].equals("0"))
            kv.put(MessageHeader.BORN_HOST, elems[3]);

        if (!elems[4].equals("0"))
            kv.put(MessageHeader.STORE_TIMESTAMP, elems[4]);

        if (!elems[5].equals("0"))
            kv.put(MessageHeader.STORE_HOST, elems[5]);

        if (!elems[6].equals("0"))
            kv.put(MessageHeader.START_TIME, elems[6]);

        if (!elems[7].equals("0"))
            kv.put(MessageHeader.STOP_TIME, elems[7]);

        if (!elems[8].equals("0"))
            kv.put(MessageHeader.TIMEOUT, elems[8]);

        if (!elems[9].equals("-1"))
            kv.put(MessageHeader.PRIORITY, elems[9]);

        if (!elems[10].equals("0"))
            kv.put(MessageHeader.RELIABILITY, elems[10]);

        if (!elems[11].equals("0"))
            kv.put(MessageHeader.SEARCH_KEY, elems[11]);

        if (!elems[12].equals("0"))
            kv.put(MessageHeader.SCHEDULE_EXPRESSION, elems[12]);

        if (!elems[13].equals("0"))
            kv.put(MessageHeader.SHARDING_KEY, elems[13]);

        if (!elems[14].equals("0"))
            kv.put(MessageHeader.SHARDING_PARTITION, elems[14]);

        if (!elems[15].equals("0"))
            kv.put(MessageHeader.TRACE_ID, elems[15]);

        return kv;
    }

    public static byte[] toBytes(int v)
    {
        byte[] bytes = new byte[4];

        bytes[3] = (byte) (v & 0xff);
        bytes[2] = (byte) ((v >> 8) & 0xff);
        bytes[1] = (byte) ((v >> 16) & 0xff);
        bytes[0] = (byte) ((v >> 24) & 0xff);

        return bytes;
    }

    public static int toInt(byte[] bytes)
    {
        return (bytes[3] & 0xff) |
                ((bytes[2] & 0xff) << 8) |
                ((bytes[1] & 0xff) << 16) |
                ((bytes[0] & 0xff) << 24);
    }
}
